/******************************************************************
 * @version      : 1.0
 * @Author       : zhl
 * @Date         : 2023-08-09 10:11:42
 * @LastEditors  : zhl
 * @E-Mail       : zhll168@163.com
 * @Description  : 业务描述
 ******************************************************************/
#include "DLT698FrameManager.h"
#include "DLT698-Frame.h"
#include "Client-APDU.h"
#include "Server-APDU.h"
#include "Link-APDU.h"
#include "Security-APDU.h"
#include <stdio.h>
#include "dlt698_global.h"



std::weak_ptr<CConfigManager> g_dlt698_config;

DLT698FrameManager DLT698FrameManager::c_frame_manager;

DLT698FrameManager::DLT698FrameManager(/* args */){
}

DLT698FrameManager::~DLT698FrameManager(){
}

DLT698FrameManager& DLT698FrameManager::getInstance(){
    return c_frame_manager;
}
#if 0
int DLT698FrameManager::handle_clent_apdu(DLT698_FRAME& frame){
    switch (frame.apdu.apdu_tag){
        case CLIENT_CONNECT_REQUEST:
            /* code */
            break;
        case CLIENT_RELEASE_REQUEST:
            break;
        case CLIENT_GET_REQUEST:
            // Client_APDU.getInstance().parse
            break;
        case CLIENT_SET_REQUEST:
            break;
        case CLIENT_ACTION_REQUEST:
            break;
        case CLIENT_REPORT_RESPONSE:
            break;
        case CLIENT_PROXY_REQUEST:
            break;
        case CLIENT_ERROR_RESPONSE:
            break;
        default:
            break;
    }
    return 0;     
}
int DLT698FrameManager::handle_link_apdu(DLT698_FRAME& frame){
    switch (frame.apdu.apdu_tag){
        case LINK_REQUEST:
            /* code */
            break;
        case LINK_RESPONSE:
            break;
        default:
            break;
    }
    return 0;     
}

int DLT698FrameManager::handle_security_apdu(DLT698_FRAME& frame){
    switch (frame.apdu.apdu_tag){
        case SECURITY_REQUEST:
            /* code */
            break;
        case SECURITY_RESPONSE:
            break;
        default:
            break;
    }
    return 0;     
}

int DLT698FrameManager::handle_server_apdu(DLT698_FRAME& frame){
    switch (frame.apdu.apdu_tag){
        case SERVER_CONNECT_RESPONSE:
            /* code */
            break;
        case SERVER_RELEASE_RESPONSE:
            break;
        case SERVER_RELEASE_NOTIFICATION:
            break;
        case SERVER_GET_RESPONSE:
            break;
        case SERVER_SET_RESPONSE:
            break;
        case SERVER_ACTION_RESPONSE:
            break;
        case SERVER_REPORT_NOTIFICATION:
            break;
        case SERVER_PROXY_RESPONSE:
            break;
        case SERVER_ERROR_RESPONSE:
            break;
        default:
            break;
    }
    return 0;    
}
#endif

void DLT698FrameManager::init(std::weak_ptr<CConfigManager> cm){
    g_dlt698_config = cm;
}

int DLT698FrameManager::handle(uint8_t* data, int len, DLT698_BUFFER& resp){
    int ret = -1;
    DLT698_FRAME frame;
    if((NULL == data) || (NULL == resp.data)){
        return ret;
    }
    if(0 == DLT698Frame::getInstance().get_frame(data, len, frame)){
        if((NULL != frame.apdu.apdu) && (1 < frame.apdu.apdu_len)){
            DLT698_BUFFER resp_data;
            resp_data.data = (uint8_t*)malloc(APDU_MAX_LEN);
            Client_APDU::getInstance().handle(frame, resp_data);
            Server_APDU::getInstance().packet(frame, resp_data, resp);
            // handle_client_apdu(frame);
            // handle_server_apdu(frame);
            // handle_link_apdu(frame);
            // handle_security_apdu(frame);
        }
    }
    
    return ret;
}
